import { copyFileSync, existsSync, mkdirSync, writeFileSync } from "fs";
import { basename, join } from "path";
import * as rimraf from 'rimraf';
import { cpDir, createManifest } from "../utils/FileUtil";
import { mergeJs, paserHtml, saveWebHtml } from "./PaserHtmlUtil";
import { PublishBase } from "./PublishBase";

export class WebPublish1 extends PublishBase {
    get projDir(){return "web"}
    createProject() {
        let verPath = join(this._publishVo.toPath, this._version);
        if (existsSync(verPath)) {
            rimraf(verPath, (err) => {
                if (err) {
                    console.warn(`删除${verPath}出错`);
                    return;
                }
                this.doPack();
            });
        } else {
            this.doPack();
        }
    }


    doPack() {
        let verPath = join(this._publishVo.toPath, this._version);
        if (!existsSync(verPath)) {
            mkdirSync(verPath);
        }
    
        // 解析html文件
        let htmlObj = paserHtml(this._publishVo.fromPath);
    
        this.log('开始修改config.json');
        // 写入config
        let crtProjPath = join(this._publishVo.toPath, this._version);
        writeFileSync(join(crtProjPath, this.getConfigName()), this.config);
    
        this.log('开始合并js代码');
        // 合并js列表
        mergeJs(htmlObj.js, this._publishVo.fromPath, crtProjPath, 'code.js', (codeName: string) => {
            this.log('开始生成index.html文件');
            let cn = basename(codeName);
            this.createHtml(htmlObj.html, cn, crtProjPath, () => {
                this.log('开始hash素材');
                this.cpRes(crtProjPath);
            });
        }, this._publishVo.content.chkRelease);
    }
    
    createHtml(htmlTxt: string, codeName: string, htmlPath: string, callback?: () => void) {
        let codeTemplate = `<script type="text/javascript" src="${codeName}"></script>\n`;
        let tempIdx = htmlTxt.indexOf('</body>');
        htmlTxt = htmlTxt.substr(0, tempIdx) + codeTemplate + htmlTxt.substr(tempIdx);
        // 将index.html写入web目录
        saveWebHtml(htmlTxt, htmlPath, () => {
            let loginPath = join(this._publishVo.fromPath, 'login.html');
            if (existsSync(loginPath)) {
                copyFileSync(loginPath, join(htmlPath, 'login.html'));
            }
            if (callback !== null) {
                callback();
            }
        });
    }
    
    cpRes(resPath: string) {
        if (!existsSync(resPath)) {
            mkdirSync(resPath);
        }
        // 拷贝素材
        cpDir(join(this._publishVo.fromPath, 'res'), join(resPath, 'res'), () => {
            createManifest(join(resPath, 'res'), (info) => {
                console.log('info;', info);
                if (info === null) {
                    console.log(`素材manifest: res_${this._version}.json 生成出错`);
                    return;
                }
                writeFileSync(join(resPath, `res_${this._version}.json`), JSON.stringify(info));
                this.log('素材拷贝完毕');
                this.packOver();
            }, this._publishVo.content.chkRelease ? 8 : 0); // 这里要注意最后的参数0, 如果不需要hash, 则为0, 需要结合chkRelease来判断
    
        }, this._publishVo.content.chkRelease, { dir: ['\W+\.svn'] });
    }
}